package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.NotelistDAO;
import com.dao.lx.RepairorderDAO;
import com.dao.lx.UserDAO;
import com.entity.Notelist;
import com.entity.Process;
import com.entity.Repairorder;
import com.entity.User;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class NotelistService {
	@Autowired
	private NotelistDAO notelistDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RepairorderDAO repairorderDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param notelist
	 * @throws ServiceException
	 */
	public void addAndUpdateNotelist(Notelist notelist) throws ServiceException{
		try {
			//判断用户是否存在
			if(notelist.getUserId()!=null){
				User user=userDAO.findOne(notelist.getUserId());
				if(user!=null){
					notelist.setUser(user);
				}else{
					notelist.setUser(null);
				}
			}
			//判断维修工单是否存在
			if(notelist.getRepairorderId()!=null){
				Repairorder repairorder=repairorderDAO.findOne(notelist.getRepairorderId());
				if(repairorder!=null){
					notelist.setRepairorder(repairorder);
				}else{
					notelist.setRepairorder(null);
				}
			}
			Notelist notelist2=notelistDAO.findOne(notelist.getNotelistId());
			if(notelist2==null){
				notelist.setNotelistIfdelete(new BigDecimal(1));
			}
			notelist.setNotelistIntodate(new Date());
			notelistDAO.save(notelist);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param notelistId
	 * @throws ServiceException
	 */
	public void deleteNotelist(Long notelistId) throws ServiceException{
		try {
			notelistDAO.delete(notelistId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 单条查询
	 * @param notelistId
	 * @return
	 */
	public Notelist findOne(Long notelistId) {
		return notelistDAO.findOne(notelistId);
	}
	
	/**
	 * 根据维修工单id查询
	 * @param notelistId
	 * @return
	 */
	public List<Notelist> findAllByRepairorderId(Long repairorderId) {
		Repairorder repairorder=repairorderDAO.findOne(repairorderId);
		return notelistDAO.queryByRepairorder(repairorder);
	}

}
